#!/bin/bash

if [ ! -n "$1" ] || [ ! -n "$2" ] || [ ! -n "$3" ] || [ ! -n "$4" ] || [ ! -n "$5" ] || [ ! -n "$6" ] || [ ! -n "$7" ]; then
        echo 'ERROR! addSchool必要参数不能为空！参数要求: serverAddress,serverInnerAddress, schoolName, schoolCode, schoolKey, globalBXIKey, userEncryptkey'
        exit -1;
fi
serverAddress=$1
serverInnerAddress=$2
schoolName=$3
schoolCode=$4
schoolKey=$5
globalBXIKey=$6
userEncryptkey=$7

mysqlHost=`echo $schoolCode | tr '[A-Z]' '[a-z]'`
if [ -z "$mysqlHost" ]; then
        echo "ERROR! mysqlHost IS NULL."
        exit -1;
fi
mysqlHost=$mysqlHost"-mysql-m"

BX_HOME=/var/nas/$schoolCode
if [ -d "$BX_HOME" ]; then
        echo "EXISTS! 学校 $schoolCode 文件已存在. 请检查后重试."
        exit -1;
fi
#mkdir /var/nas/
if [ ! -d "/var/nas/model" ];then
        echo "ERROR! model path is not exists."
        exit -1;
fi
cp -r /var/nas/model $BX_HOME
test ! -d "$BX_HOME" && echo "$BX_HOME not found! cp model failed." && exit -1 || ;
sed -i "s/{server.address}/$serverAddress/g" $BX_HOME/config/common.properties
sed -i "s/{server.innerAddress}/$serverInnerAddress/g" $BX_HOME/config/common.properties
sed -i "s/{school.name}/$schoolName/g" $BX_HOME/config/common.properties
sed -i "s/{ibc.schoolCode}/$schoolCode/g" $BX_HOME/config/common.properties
sed -i "s/{ibc.schoolKey}/$schoolKey/g" $BX_HOME/config/common.properties
sed -i "s/{bxi_global_bxi_key}/$globalBXIKey/g" $BX_HOME/config/common.properties
sed -i "s/{host.base}/$schoolCode/g" $BX_HOME/config/common.properties
sed -i "s/{user.encrypt.key}/$userEncryptkey/g" $BX_HOME/config/uic.properties
sed -i "s/{host.mysql}/$mysqlHost/g" $BX_HOME/config/*.properties

echo "SUCCESS! ending config school $schoolCode."
